#include "inc/god.h"

#define SERVER_IP "0.0.0.0"
#define MYPORT  12100

#define QUEUE   20
#define MSG_LENGTH 1024

void handleSignal(int signal) {
    if (signal == SIGINT) {
        printf("Program Exit\n");
        exit(0);
    }
}

int main(int argc, char *argv[])
{
    signal(SIGINT, handleSignal);
    int sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);

    struct sockaddr_in svraddr, cliaddr;
    svraddr.sin_family = AF_INET;
    svraddr.sin_port = htons(12100);
    svraddr.sin_addr.s_addr = inet_addr(SERVER_IP);
    //svraddr.sin_addr.s_addr = htonl(INADDR_ANY);

    socklen_t svrlen, clilen;

    svrlen = sizeof(struct sockaddr_in);

    clilen = sizeof(struct sockaddr_in);

    int nbind = bind(sockfd, (struct sockaddr*)&svraddr, svrlen);
    if(nbind == 0){

    } else{

    }
    printf("svrlen %d %d %d\n", sizeof(struct sockaddr_in), sizeof(struct sockaddr), nbind);

    char buff[512];
    memset(buff, 0 , sizeof(buff));
    int getLen = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr*)&cliaddr, &clilen);
    // 打印客户端的IP地址和端口

    char client_ip[INET_ADDRSTRLEN];       // #define INET_ADDRSTRLEN 16

    inet_ntop(AF_INET, &cliaddr.sin_addr, client_ip, INET_ADDRSTRLEN);
    //printf("From: %s:%d ", client_ip, ntohs(cliaddr.sin_port));

    printf("[Len:%d, Info: (%s)]\n",getLen, buff);

    char resBuff[512];
    memset(resBuff, 0, sizeof(resBuff));

    strcpy(resBuff, "C-<-<-S");
    sendto(sockfd, resBuff , sizeof(resBuff), 0, (struct sockaddr*)&cliaddr, clilen);

    return 0;
}

